// CodeCompletion.cs created with MonoDevelop
// User: kpr at 11:18 03.11.2007
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//

using System;

namespace Designer
{
	public class CodeCompletion
	{
		CodeCompletionContext currentCompletionContext;
		ICompletionWidget completionWidget;
		bool autoHideCompletionWindow = true;
		
		public CodeCompletion(ICompletionWidget completionWidget)
		{
			this.completionWidget = completionWidget;
			if (completionWidget != null)
				completionWidget.CompletionContextChanged += OnCompletionContextChanged;
		}
		public bool KeyPress (Gdk.Key key, Gdk.ModifierType modifier)
		{
						
			if (currentCompletionContext != null) {
				autoHideCompletionWindow = false;
				if (CompletionListWindow.ProcessKeyEvent (key, modifier)) {
					autoHideCompletionWindow = true;
					return true;
				}
				autoHideCompletionWindow = false;
			}
//			
//			if (ParameterInformationWindowManager.IsWindowVisible) {
//				if (ParameterInformationWindowManager.ProcessKeyEvent (key, modifier))
//					return true;
//				autoHideCompletionWindow = false;
//			}
//			
//
			// Handle code completion
			
			if (completionWidget != null && currentCompletionContext == null) {
				Console.WriteLine("Handling code completion");
				currentCompletionContext = completionWidget.CreateCodeCompletionContext (completionWidget.CursorPosition);
				
				CompletionListWindow.ShowWindow (completionWidget, currentCompletionContext, OnCompletionWindowClosed);
//				ICompletionDataProvider cp = HandleCodeCompletion (currentCompletionContext, (char)(uint)key);
//					
//				if (cp != null)
					//CompletionListWindow.ShowWindow ((char)(uint)key, cp, completionWidget, currentCompletionContext, OnCompletionWindowClosed);
//				else
//					currentCompletionContext = null;
			}
//			
//			// Handle parameter completion
//			
//			if (ParameterInformationWindowManager.IsWindowVisible)
//				ParameterInformationWindowManager.PostProcessKeyEvent (key, modifier);
//
//			if (completionWidget != null) {
//				ICodeCompletionContext ctx = completionWidget.CreateCodeCompletionContext (Editor.CursorPosition);
//				IParameterDataProvider paramProvider = HandleParameterCompletion (ctx, (char)(uint)key);
//				if (paramProvider != null)
//					ParameterInformationWindowManager.ShowWindow (ctx, paramProvider);
//			}
//			
//			autoHideCompletionWindow = true;
//			
			return true;
		}
		void OnCompletionContextChanged (object o, EventArgs a)
		{
			if (autoHideCompletionWindow) {
				if (currentCompletionContext != null)
				{
					CompletionListWindow.HideWindow ();
					Console.WriteLine("Hiding window");
				}
				//ParameterInformationWindowManager.HideWindow ();
			}
		}
		
		void OnCompletionWindowClosed ()
		{
			currentCompletionContext = null;
		}
	}
}
